require("../modules/connectDB.js");

var sch = require('../modules/schema.js');

var Teacher = sch.Teacher;
var SC = sch.SC;
var Course = sch.Course;
var Key = sch.Key;

SC.find({"course":"55ef853b9dbb070d2efe901c"},function(err,scs){

	var data = [];
	scs.forEach(function(v){
		if(!v.signup||v.signup.length <= 0){
			return;
		}
		var signup = v.signup[0];
		signup.time += 1;
		data.push({
			id:v.student,
			signup:signup
		});
	});
	SC.find({course:"55ef84cb9dbb070d2efe901a"},function(err,s){
		s.forEach(function(v1){
			data.forEach(function(v2){
				if(v1.student == v2.id){
					v1.signup.push(v2.signup);
					v1.save(function(err){
						if(err)console.log(err);
					});
				}
			});
		});
	});
});

Key.find({"tmp":"089beb0d"},function(err,keys){
	keys.forEach(function(v){
		v.time += 1;
		v.cid = "55ef84cb9dbb070d2efe901a";
		v.save(function(err){
			if(err)console.log(err);
		});
	});

});

Course.find({"_id":"55ef84cb9dbb070d2efe901a"},function(err,cs1){
	var c1 = cs1[0];
	Course.find({"_id":"55ef853b9dbb070d2efe901c"},function(err,cs2){
		var c2 = cs2[0];
		var time = c2.time_each[0];
		time.times += 1;
		c1.time_each.push(time);
		c1.signup_times += 1;
		c1.save(function(err){
			if(err)console.log(err);
		});
	});
});
